package com.ymatou.iapi.mqdispatch1.testcase;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.mqdispatch1.parameter.DispatchBean;
import com.ymatou.iapi.mqdispatch1.service.DispatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.mongo.MQMessageBean;
import com.ymttest.database.mongo.MQMessageCompensateBean;
import com.ymttest.database.mongo.MQMessageCompensateWapper;
import com.ymttest.database.mongo.MQMessageStatusBean;
import com.ymttest.database.mongo.MQMessageStatusWapper;
import com.ymttest.database.mongo.MQMessageWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 分发消息用例 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_dispatch {
	private static DispatchBean dispatchBean;
	private static DispatchCall dispatchCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("分发消息用例");
	}

	@Before
	public void caseUp() {
		dispatchBean = new DispatchBean();
		dispatchCall = new DispatchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(dispatchCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_001() {
		Logger.start(true, "分发消息-接收成功");
		try {

			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String appName = "testbamei";
			String codeName = "qusi";

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String busIp = "127.0.0.1";

			Date nowTime = new Date();
			Date date = new Date(nowTime.getTime() - 8 * 60 * 60 * 1000);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			String dateStr = format.format(nowTime);

			String json = "{\"uuid\" : \"" + uuid + "\",\"aid\" : \"" + appName + "\",\"code\" : \"" + codeName + "\","
					+ "\"mid\" : \"" + msguniqueid + "\",\"ip\" : \"127.0.0.1\",\"busIp\" : \"" + busIp + "\","
					+ "\"body\" : \"\\\"" + body + "\\\"\",\"ctime\" : {$date:\"" + dateStr + "\"},"
					+ "\"pushstatus\" : 1000,\"nstatus\" : 0,\"pstatus\" : 0," + "\"pushtime\" : {$date:\"" + dateStr
					+ "\"}}";
			//Logger.comment(json);

			new MQMessageWapper().saveData(appName, codeName, json);

			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setMessageId(msguniqueid);
			dispatchBean.setMessageUuid(uuid);
			dispatchBean.setMessageBody(body);

			List<MQMessageStatusBean> statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num1 = statusList.size();

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, dispatchCall.getCode(), "验证Code");
			Logger.verifyEquals("NULL", dispatchCall.getMsg().toUpperCase(), "验证Msg");

			MQMessageBean messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);

			int flag = 0;

			while (messageBean == null || messageBean.getPstatus() != 1 && flag < 200) {
				Thread.sleep(200);
				flag++;
				messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);
			}

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH");

			// 验证Message表
			Logger.verifyEquals(appName, messageBean.getAid(), "验证数据库Aid");
			Logger.verifyEquals(codeName, messageBean.getCode(), "验证数据库Code");
			Logger.verifyEquals("\"" + body + "\"", messageBean.getBody(), "验证数据库Body");
			Logger.verifyEquals(busIp, messageBean.getIp(), "验证数据库IP");
			Logger.verifyEquals(sdf.format(nowTime), messageBean.getCtime().toString().subSequence(7, 20),
					"验证数据库Ctime");
			Logger.verifyEquals(busIp, messageBean.getBusIp(), "验证数据库BusIp");

			Logger.verifyEquals(0, messageBean.getNstatus(), "验证数据库nstatus");
			Logger.verifyEquals(sdf.format(date),messageBean.getPushtime().toString().subSequence(7, 20), "验证数据库Pushtime");
			Logger.verifyEquals(1, messageBean.getPstatus(), "验证数据库Pushstatus:1-分发成功");

			// 验证Status表生成记录
			statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num2 = statusList.size();

			Logger.verifyEquals(1, num2 - num1, "验证数据库MQ_Message_Status新生成一条记录");

			MQMessageStatusBean status = statusList.get(num2 - 1);

			Logger.verifyEquals(messageBean.getUuid(), status.getUuid(), "验证数据库MQ_Message_Status字段uuid");
			Logger.verifyEquals(messageBean.getMid(), status.getMid(), "验证数据库MQ_Message_Status字段Mid");
			Logger.verifyEquals("bamei_qusi_c1", status.getCid(), "验证数据库MQ_Message_Status字段Cid");
			Logger.verifyEquals("PushOk", status.getStatus(), "验证数据库MQ_Message_Status字段Status");
			Logger.verifyEquals("Dispatch", status.getSource(), "验证数据库MQ_Message_Status字段Source");
			Logger.verifyEquals(sdf.format(date), status.getCtime().toString().subSequence(7, 20),
					"验证数据库MQ_Message_Status字段Ctime");
			Logger.verifyEquals(true, status.getResult().contains("ok, bamei_qusi_c1"),
					"验证数据库MQ_Message_Status字段Result");
			Logger.verifyEquals(true,
					status.getResult().contains("http://mockforpay.iapi.ymatou.com/api/messagebus/return/ok"),
					"验证数据库MQ_Message_Status字段Result");
			Logger.verifyEquals(true, status.getR_ip().contains("134"), "验证数据库MQ_Message_Status字段R_ip");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_002() {
		Logger.start(true, "分发消息-接收失败");
		try {

			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String appName = "testbamei";
			String codeName = "youqusi";

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String busIp = "127.0.0.1";

			Date nowTime = new Date();
			Date date = new Date(nowTime.getTime() - 8 * 60 * 60 * 1000);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			String dateStr = format.format(nowTime);

			String json = "{\"uuid\" : \"" + uuid + "\",\"aid\" : \"" + appName + "\",\"code\" : \"" + codeName + "\","
					+ "\"mid\" : \"" + msguniqueid + "\",\"ip\" : \"127.0.0.1\",\"busIp\" : \"" + busIp + "\","
					+ "\"body\" : \"\\\"" + body + "\\\"\",\"ctime\" : {$date:\"" + dateStr + "\"},"
					+ "\"pushstatus\" : 1000,\"nstatus\" : 0,\"pstatus\" : 0," + "\"pushtime\" : {$date:\"" + dateStr
					+ "\"}}";
			Logger.comment(json);

			new MQMessageWapper().saveData(appName, codeName, json);

			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setMessageId(msguniqueid);
			dispatchBean.setMessageUuid(uuid);
			dispatchBean.setMessageBody(body);

			List<MQMessageStatusBean> statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num1 = statusList.size();

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, dispatchCall.getCode(), "验证Code");
			Logger.verifyEquals("NULL", dispatchCall.getMsg().toUpperCase(), "验证Msg");

			MQMessageBean messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);

			int flag = 0;

			while (messageBean == null || messageBean.getPstatus() != 2 && flag < 200) {
				Thread.sleep(200);
				flag++;
				messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);
			}

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH");

			// 验证Message表
			Logger.verifyEquals(appName, messageBean.getAid(), "验证数据库Aid");
			Logger.verifyEquals(codeName, messageBean.getCode(), "验证数据库Code");
			Logger.verifyEquals("\"" + body + "\"", messageBean.getBody(), "验证数据库Body");
			Logger.verifyEquals(busIp, messageBean.getIp(), "验证数据库IP");
			Logger.verifyEquals(sdf.format(nowTime), messageBean.getCtime().toString().subSequence(7, 20),
					"验证数据库Ctime");
			Logger.verifyEquals(busIp, messageBean.getBusIp(), "验证数据库BusIp");

			Logger.verifyEquals(2, messageBean.getNstatus(), "验证数据库nstatus:2-分发进入补单");
			Logger.verifyEquals(sdf.format(date),messageBean.getPushtime().toString().subSequence(7, 20), "验证数据库Pushtime");
			Logger.verifyEquals(2, messageBean.getPstatus(), "验证数据库Pushstatus:2-补单中");

			// 验证进入补单库
			MQMessageCompensateBean compensate = new MQMessageCompensateWapper().getCompensateByUuid(appName, codeName,
					messageBean.getUuid());

			Logger.verifyEquals(messageBean.getAid(), compensate.getAppid(), "验证数据库MQ_Message_Compensate字段AppId");
			Logger.verifyEquals(messageBean.getCode(), compensate.getCode(), "验证数据库MQ_Message_Compensate字段Code");
			Logger.verifyEquals(messageBean.getMid(), compensate.getMid(), "验证数据库MQ_Message_Compensate字段mId");
//			Logger.verifyEquals(sdf.format(date), compensate.getRtimeout().toString().subSequence(7, 20),
//					"验证数据库MQ_Message_Compensate字段Rtimeout");
			Logger.verifyEquals(0, compensate.getRetrycount(), "验证数据库MQ_Message_Compensate字段RetryCount");
			Logger.verifyEquals(2, compensate.getSource(), "验证数据库MQ_Message_Compensate字段Source补单来源:2-分发站");
			Logger.verifyEquals(0, compensate.getNstatus(), "验证数据库MQ_Message_Compensate字段nStatus");
			Logger.verifyEquals("bamei_qusi_c1", compensate.getCid(), "验证数据库MQ_Message_Compensate字段Cid");

			// 验证Status表生成记录
			statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num2 = statusList.size();

			Logger.verifyEquals(1, num2 - num1, "验证数据库MQ_Message_Status新生成一条记录");

			MQMessageStatusBean status = statusList.get(num2 - 1);

			Logger.verifyEquals(messageBean.getUuid(), status.getUuid(), "验证数据库MQ_Message_Status字段uuid");
			Logger.verifyEquals(messageBean.getMid(), status.getMid(), "验证数据库MQ_Message_Status字段Mid");
			Logger.verifyEquals("bamei_qusi_c1", status.getCid(), "验证数据库MQ_Message_Status字段Cid");
			Logger.verifyEquals("PushFail", status.getStatus(), "验证数据库MQ_Message_Status字段Source");
			Logger.verifyEquals("Dispatch", status.getSource(), "验证数据库MQ_Message_Status字段uuid");
			Logger.verifyEquals(sdf.format(date), status.getCtime().toString().subSequence(7, 20),
					"验证数据库MQ_Message_Status字段Ctime");
			Logger.verifyEquals(true, status.getResult().contains("fail, bamei_qusi_c1"),
					"验证数据库MQ_Message_Status字段Result");
			Logger.verifyEquals(true,
					status.getResult().contains("http://mockforpay.iapi.ymatou.com/api/messagebus/return/fail"),
					"验证数据库MQ_Message_Status字段Result");
			Logger.verifyEquals(true, status.getR_ip().contains("134"), "验证数据库MQ_Message_Status字段R_ip");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_003() {
		Logger.start(false, "分发消息-AppId空");
		try {
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String appName = "testbamei";
			String codeName = "qusi";
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);

			//dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setMessageId(msguniqueid);
			dispatchBean.setMessageUuid(uuid);
			dispatchBean.setMessageBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("400", dispatchCall.getCode(), "验证Code");
			Logger.verifyEquals("appId不能为空", dispatchCall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_004() {
		Logger.start(false, "分发消息-Code空");
		try {
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String appName = "testbamei";
			String codeName = "qusi";
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);

			dispatchBean.setAppId(appName);
			//dispatchBean.setCode(codeName);
			dispatchBean.setMessageId(msguniqueid);
			dispatchBean.setMessageUuid(uuid);
			dispatchBean.setMessageBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("400", dispatchCall.getCode(), "验证Code");
			Logger.verifyEquals("CODE不能为空", dispatchCall.getMsg().toUpperCase(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_005() {
		Logger.start(false, "分发消息-mid空");
		try {
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String appName = "testbamei";
			String codeName = "qusi";
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);

			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			//dispatchBean.setMessageId(msguniqueid);
			dispatchBean.setMessageUuid(uuid);
			dispatchBean.setMessageBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("400", dispatchCall.getCode(), "验证Code");
			Logger.verifyEquals("MESSAGEID CAN NOT BE EMPTY.", dispatchCall.getMsg().toUpperCase(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_006() {
		Logger.start(false, "分发消息-uuid空");
		try {
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String appName = "testbamei";
			String codeName = "qusi";
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);

			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setMessageId(msguniqueid);
			//dispatchBean.setMessageUuid(uuid);
			dispatchBean.setMessageBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("400", dispatchCall.getCode(), "验证Code");
			Logger.verifyEquals("MESSAGEUUID CAN NOT BE EMPTY.", dispatchCall.getMsg().toUpperCase(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_007() {
		Logger.start(false, "分发消息-body可空");
		try {
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String appName = "testbamei";
			String codeName = "qusi";
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);

			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setMessageId(msguniqueid);
			dispatchBean.setMessageUuid(uuid);
			//dispatchBean.setMessageBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("200", dispatchCall.getCode(), "验证Code");
			Logger.verifyEquals("NULL", dispatchCall.getMsg().toUpperCase(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_008() {
		Logger.start(false, "分发消息-AppId不存在");
		try {
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String appName = "testbage";
			String codeName = "qusi";
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);

			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setMessageId(msguniqueid);
			dispatchBean.setMessageUuid(uuid);
			dispatchBean.setMessageBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("400", dispatchCall.getCode(), "验证Code");
			Logger.verifyEquals("参数异常|invalid appId:testbage", dispatchCall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_009() {
		Logger.start(false, "分发消息-Code不存在");
		try {
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String appName = "testbamei";
			String codeName = "buqusi";
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);

			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setMessageId(msguniqueid);
			dispatchBean.setMessageUuid(uuid);
			dispatchBean.setMessageBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("400", dispatchCall.getCode(), "验证Code");
			Logger.verifyEquals("参数异常|invalid appCode:testbamei_buqusi", dispatchCall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_010() {
		Logger.start(false, "分发消息-mid不存在");
		try {
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String appName = "testbamei";
			String codeName = "qusi";
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);

			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setMessageId(msguniqueid);
			dispatchBean.setMessageUuid(uuid);
			dispatchBean.setMessageBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("200", dispatchCall.getCode(), "验证Code");
			Logger.verifyEquals("null", dispatchCall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_011() {
		Logger.start(false, "分发消息-uuid不存在");
		try {
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String appName = "testbamei";
			String codeName = "qusi";
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);

			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setMessageId(msguniqueid);
			dispatchBean.setMessageUuid(uuid);
			dispatchBean.setMessageBody(body);

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals("200", dispatchCall.getCode(), "验证Code");
			Logger.verifyEquals("null", dispatchCall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_dispatch_013() {
		Logger.start(true, "分发消息-回调超时进入补单库");
		try {

			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMM");
			String dateStr1 = sDateFormat.format(new java.util.Date());

			String appName = "testbamei";
			String codeName = "zaiyouqusi";

			String uuid = dateStr1 + "." + TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String busIp = "127.0.0.1";

			Date nowTime = new Date();
			Date date = new Date(nowTime.getTime() - 8 * 60 * 60 * 1000);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			String dateStr = format.format(nowTime);

			String json = "{\"uuid\" : \"" + uuid + "\",\"aid\" : \"" + appName + "\",\"code\" : \"" + codeName + "\","
					+ "\"mid\" : \"" + msguniqueid + "\",\"ip\" : \"127.0.0.1\",\"busIp\" : \"" + busIp + "\","
					+ "\"body\" : \"\\\"" + body + "\\\"\",\"ctime\" : {$date:\"" + dateStr + "\"},"
					+ "\"pushstatus\" : 1000,\"nstatus\" : 0,\"pstatus\" : 0," + "\"pushtime\" : {$date:\"" + dateStr
					+ "\"}}";
			Logger.comment(json);

			new MQMessageWapper().saveData(appName, codeName, json);

			dispatchBean.setAppId(appName);
			dispatchBean.setCode(codeName);
			dispatchBean.setMessageId(msguniqueid);
			dispatchBean.setMessageUuid(uuid);
			dispatchBean.setMessageBody(body);

			List<MQMessageStatusBean> statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num1 = statusList.size();

			dispatchCall.setData(dispatchBean);
			dispatchCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, dispatchCall.getCode(), "验证Code");
			Logger.verifyEquals("NULL", dispatchCall.getMsg().toUpperCase(), "验证Msg");

			MQMessageBean messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);

			int flag = 0;

			while (messageBean == null || messageBean.getPstatus() != 2 && flag < 200) {
				Thread.sleep(200);
				flag++;
				messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);
			}

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH");

			// 验证Message表
			Logger.verifyEquals(appName, messageBean.getAid(), "验证数据库Aid");
			Logger.verifyEquals(codeName, messageBean.getCode(), "验证数据库Code");
			Logger.verifyEquals("\"" + body + "\"", messageBean.getBody(), "验证数据库Body");
			Logger.verifyEquals(busIp, messageBean.getIp(), "验证数据库IP");
			Logger.verifyEquals(sdf.format(nowTime), messageBean.getCtime().toString().subSequence(7, 20),
					"验证数据库Ctime");
			Logger.verifyEquals(busIp, messageBean.getBusIp(), "验证数据库BusIp");

			Logger.verifyEquals(2, messageBean.getNstatus(), "验证数据库nstatus:2-分发进入补单");
			Logger.verifyEquals(sdf.format(date),messageBean.getPushtime().toString().subSequence(7, 20), "验证数据库Pushtime");
			Logger.verifyEquals(2, messageBean.getPstatus(), "验证数据库Pushstatus:2-补单中");

			// 验证进入补单库
			MQMessageCompensateBean compensate = new MQMessageCompensateWapper().getCompensateByUuid(appName, codeName,
					messageBean.getUuid());

			Logger.verifyEquals(messageBean.getAid(), compensate.getAppid(), "验证数据库MQ_Message_Compensate字段AppId");
			Logger.verifyEquals(messageBean.getCode(), compensate.getCode(), "验证数据库MQ_Message_Compensate字段Code");
			Logger.verifyEquals(messageBean.getMid(), compensate.getMid(), "验证数据库MQ_Message_Compensate字段mId");
			Logger.verifyEquals(sdf.format(date), compensate.getRtimeout().toString().subSequence(7, 20),
					"验证数据库MQ_Message_Compensate字段Rtimeout");
			Logger.verifyEquals(0, compensate.getRetrycount(), "验证数据库MQ_Message_Compensate字段RetryCount");
			Logger.verifyEquals(2, compensate.getSource(), "验证数据库MQ_Message_Compensate字段Source补单来源:2-分发站");
			Logger.verifyEquals(0, compensate.getNstatus(), "验证数据库MQ_Message_Compensate字段nStatus");
			Logger.verifyEquals("bamei_zaiyouqusi_c1", compensate.getCid(), "验证数据库MQ_Message_Compensate字段Cid");

			// 验证Status表生成记录
			statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num2 = statusList.size();

			Logger.verifyEquals(1, num2 - num1, "验证数据库MQ_Message_Status新生成一条记录");

			MQMessageStatusBean status = statusList.get(num2 - 1);

			Logger.verifyEquals(messageBean.getUuid(), status.getUuid(), "验证数据库MQ_Message_Status字段uuid");
			Logger.verifyEquals(messageBean.getMid(), status.getMid(), "验证数据库MQ_Message_Status字段Mid");
			Logger.verifyEquals("bamei_zaiyouqusi_c1", status.getCid(), "验证数据库MQ_Message_Status字段Cid");
			Logger.verifyEquals("PushFail", status.getStatus(), "验证数据库MQ_Message_Status字段Source");
			Logger.verifyEquals("Dispatch", status.getSource(), "验证数据库MQ_Message_Status字段uuid");
			Logger.verifyEquals(sdf.format(date), status.getCtime().toString().subSequence(7, 20),
					"验证数据库MQ_Message_Status字段Ctime");
			Logger.verifyEquals(true, status.getResult().contains("fail, bamei_zaiyouqusi_c1"),
					"验证数据库MQ_Message_Status字段Result");
			Logger.verifyEquals(true,
					status.getResult().contains("http://mockforpay.iapi.ymatou.com/api/messagebus/delay/5000"),
					"验证数据库MQ_Message_Status字段Result");
			Logger.verifyEquals(true, status.getR_ip().contains("134"), "验证数据库MQ_Message_Status字段R_ip");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}